const User = require("../../models/user.model");
class UserSevice {
  async createUser({
    user_name,
    password,
    gender,
    avatar,
    introduction,
    is_admin,
  }) {
    // 插入数据

    //await表达式返回值: promise对象的值
    const res = await User.create({
      user_name,
      password,
      gender,
      avatar,
      introduction,
      is_admin,
    });
    // console.log(res);
    //把res作为promise对象来返回
    return res.dataValues;
  }

  async getUserInfo({ id, user_name, password, is_admin }) {
    const whereOpt = {};

    //短路  如果id存在就执行后边
    id && Object.assign(whereOpt, { id });
    user_name && Object.assign(whereOpt, { user_name });
    password && Object.assign(whereOpt, { password });
    is_admin && Object.assign(whereOpt, { is_admin });

    //查询第一个满足条件的记录
    const res = await User.findOne({
      attributes: [
        "id",
        "user_name",
        "password",
        "gender",
        "introduction",
        "avatar",
        "is_admin",
      ],
      where: whereOpt,
    });

    return res ? res.dataValues : null;
  }

  async updateById({
    id,
    user_name,
    gender,
    introduction,
    password,
    avatar,
    is_admin,
  }) {
    try {
      // console.log(id, user_name, gender, introduction);

      // 验证 id 是否存在
      if (!id) {
        throw new Error("更新用户信息时必须提供 id");
      }

      // 定义更新条件
      const whereOptions = { id };
      // 存储需要更新的用户信息
      const updatedUser = {};

      // 添加非空字段到 updatedUser 对象
      if (user_name) Object.assign(updatedUser, { user_name });
      if (password) Object.assign(updatedUser, { password });
      if (is_admin !== undefined) Object.assign(updatedUser, { is_admin });
      if (gender) Object.assign(updatedUser, { gender });
      if (introduction) Object.assign(updatedUser, { introduction });
      if (avatar) Object.assign(updatedUser, { avatar });

      // 执行数据库更新操作
      const result = await User.update(updatedUser, { where: whereOptions });

      // 根据更新结果返回布尔值
      return result[0] > 0;
    } catch (error) {
      // 处理错误
      console.error("更新用户信息时出现错误:", error);
      return false;
    }
  }

  async findUsers(pageNum, pageSize) {
    // //获取总数
    // const count = await Goods.count();
    // //获取分页的数据
    // const offset = (pageNum - 1) * pageSize;
    // const rows = await Goods.findAll({ offset: offset, limit: pageSize * 1 });

    const offset = (pageNum - 1) * pageSize;
    const { count, rows } = await User.findAndCountAll({
      offset: offset,
      limit: pageSize * 1,
      attributes: [
        "id",
        "user_name",
        "is_admin",
        "avatar",
        "gender",
        "introduction",
      ],
    });

    return {
      pageNum,
      pageSize,
      total: count,
      list: rows,
    };
  }

  async deleteUser(id) {
    const res = await User.destroy({ where: { id } });

    return res > 0 ? true : false;
  }

  async findAUser(id) {
    const res = await User.findOne({ where: { id } });

    return res !== null ? res : null;
  }

  async updateUser(id, user) {
    const res = await User.update(user, { where: { id } });

    return res > 0 ? true : false;
  }
}

module.exports = new UserSevice();
